﻿using System;
using System.Linq;

namespace LogikBug.ModularMvc.Hosting
{
	public static class AppResourceHelper
	{
		#region Methods

		private static ModularHttpContext Context
		{
			get
			{
				return ModularHttpContext.Current;
			}
		}

		public static string ConvertToAppResourcePath(string path)
		{
			if (Context.Module == null) { return string.Empty; }

			return string.Concat(
				Context.Module.Assembly.GetName().Name,
				path.ThrowIfNull().Trim('~').Replace('/', '.')
			);
		}

		public static bool IsAppResourcePath(string virtualPath)
		{
			if (Context.Module == null) { return false; }

			var resourcePath = ConvertToAppResourcePath(virtualPath);
			return Context.Module.Resources.Any(r => r.Equals(resourcePath, StringComparison.OrdinalIgnoreCase));
		}

		#endregion
	}
}
